package com.dcms.staticizing.controller;

import com.dcms.staticizing.service.IStaticizingService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * ClassName: StaticizingController
 * 所属功能模块: 网页静态化的controller
 * 功能描述:
 *
 * @author zhouyijin
 * @Date 2018年02月19日 17:37
 * @since Ver 1.0
 */
@Controller
@RequestMapping("staticizing")
public class StaticizingController {

    private static final Logger LOGGER = Logger.getLogger(StaticizingController.class);

    @Autowired //目前只有一个实现: SimpleStaticizingService.java
    private IStaticizingService staticizingService;

    /**
     * 将指定url页面静态化
     * @param request
     * @param url   url是需要静态化页面的相对路径 比如"http://111.112:8080/dfadf"是绝对路径
     */
    @RequestMapping("staticizePage")
    @ResponseBody
    public ResultSet staticizePage(HttpServletRequest request,@NotNull @RequestParam("url")String url){
        ResultSet rs = new ResultSet();
        try {
            staticizingService.staticizing(request,url);
            rs.setResultCode(1);
            rs.setResultMessage("静态化网页操作成功!");
            rs.setResultObject(url);
        }catch (Exception e){
            rs.setResultCode(0);
            rs.setResultMessage("静态化网页操作失败!");
            rs.setResultObject(e.getMessage());
        }
        return rs;
    }

    /**
     * 将指定的 多个 url页面静态化
     * @param request
     * @param urls urls是需要静态化页面的相对路径数组 比如"http://111.112:8080/dfadf"是绝对路径
     */
    @RequestMapping("staticizePages")
    @ResponseBody
    public ResultSet staticizePages(HttpServletRequest request,@RequestParam("url")String[] urls){
        List<ResultSet> list = new ArrayList();
        for (String url:urls) {
            list.add(staticizePage(request,url));
        }
        List<String> successUrl = new ArrayList();
        for (ResultSet resultSet:list) {
            if (resultSet.getResultCode()==1){
                successUrl.add(resultSet.getResultObject().toString());
            }
        }
        ResultSet result = new ResultSet();
        result.setResultCode(successUrl.size()>0 ? 1 : 0);
        result.setResultMessage("共有"+successUrl.size()+"个页面静态化成功!");
        result.setResultObject(successUrl);
        return result;
    }

    /**
     * 将指定的 url 静态化效果移除
     * 恢复原来的jsp动态生成页面
     * @param url urls是需要静态化页面的相对路径数组 比如"http://111.112:8080/dfadf"是绝对路径
     */
    @RequestMapping("removeStaticizePage")
    @ResponseBody
    public ResultSet removeStaticizePage(@RequestParam("url")String url){
        ResultSet rs = new ResultSet();
        try {
            staticizingService.removeStaticizedHtml(url);
            rs.setResultCode(1);
            rs.setResultMessage("已移除此静态化页面!");
            rs.setResultObject(url);
        }catch (Exception e){
            rs.setResultCode(0);
            rs.setResultMessage("移除失败!");
            rs.setResultObject(null);
        }
        return rs;
    }


    /**
     * 将指定的 多个 url 静态化效果移除
     * @param urls urls是需要静态化页面的相对路径数组 比如"http://111.112:8080/dfadf"是绝对路径
     */
    @RequestMapping("removeStaticizePages")
    @ResponseBody
    public ResultSet removeStaticizePages(@RequestParam("url")String[] urls){
        List<ResultSet> list = new ArrayList();
        for (String url:urls) {
            list.add(removeStaticizePage(url));
        }
        List<String> successUrl = new ArrayList();
        for (ResultSet resultSet:list) {
            if (resultSet.getResultCode()==1){
                successUrl.add(resultSet.getResultObject().toString());
            }
        }
        ResultSet result = new ResultSet();
        result.setResultCode(successUrl.size()>0 ? 1 : 0);
        result.setResultMessage("共移除"+successUrl.size()+"个页面静态化!");
        result.setResultObject(successUrl);
        return result;
    }

    /**
     * 获取所有 已经被静态化的页面的url
     * @return
     */
    @RequestMapping("getAllStaticizingUrls")
    @ResponseBody
    public Set<String> getAllStaticizingUrls(){
        Set<String> urls = null;
        try {
//            urls = new HashSet<>();
//            for (int i = 0;i<1000;i++){
//                urls.add("/da/number"+i);
//            }
            urls = staticizingService.getAllStaticizingUrls();
        }catch (Exception e){}
        return urls;
    }


    /**
     * 静态化页面功能 的管理页面
     * @return
     */
    @RequestMapping("manage")
    public String managePage(){
        return "staticizing/managePage";
    }


    public IStaticizingService getStaticizingService() {
        return staticizingService;
    }

    public void setStaticizingService(IStaticizingService staticizingService) {
        this.staticizingService = staticizingService;
    }

    /**
     * 临时用来作为结果保存的
     */
    private static class ResultSet{
        private int resultCode;
        private String resultMessage;
        private Object resultObject;

        public int getResultCode() {
            return resultCode;
        }

        public void setResultCode(int resultCode) {
            this.resultCode = resultCode;
        }

        public String getResultMessage() {
            return resultMessage;
        }

        public void setResultMessage(String resultMessage) {
            this.resultMessage = resultMessage;
        }

        public Object getResultObject() {
            return resultObject;
        }

        public void setResultObject(Object resultObject) {
            this.resultObject = resultObject;
        }
    }
}
